package com.example.mytips.jsonToFlutterEntity;

import com.example.mytips.utils.*;
import com.google.gson.Gson;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class CreateFlutterEntity extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {

        Mye mye = Mye.builder(e);

        List<String> strings = ReadFileUtil.read(mye.psiFile.getVirtualFile());
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < strings.size(); i++) {
            String line = strings.get(i);
            if (line.contains("//")) {
                int i1 = line.indexOf("//");
                line = line.substring(0, i1);
            }
            builder.append(line);
        }
        String className = SelectFileInfo.getFileName(e);

        // 将 JSON 字符串转换为 Map
        Map<String, Object> map = StrUtil.jsonToMap(builder.toString());

        if (map == null || map.isEmpty()) {
            Messages.showInfoMessage(mye.project, "JSON格式错误，比如末尾元素没有逗号，英文引号", "提示");
            return;
        }

        String string = MyEntityLog.log(className, map);
        TextInsertUtil.insertString(e, string);

    }


}
